import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import * as API from "@api/index";
import {AuthService} from "@shared/service/auth/auth.service";
import {TeacherService} from "@train/teacher/teacher.service";
import {ActivatedRoute, Params} from "@angular/router";
import {SCORE_CRITERIA_DESC_OPTIONS} from "@train/teacher/score-input/input-criteria/score-criteria.config";

@Component({
  selector: 'score-criteria',
  templateUrl: './score-criteria.component.html',
  styleUrls: ['./score-criteria.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ScoreCriteriaComponent implements OnInit {
  public testScoreOptions;

  public gradeList = [];
  public descInfo;
  public dataStore = {
    pageTitle: '',
    moduleTitle: '',
    unitName: ''
  };

  constructor(private teacherService: TeacherService,
              private route: ActivatedRoute,
              private authService: AuthService) {
    this.route.params
      .switchMap((params: Params) => {
        return this.route.queryParams;
      }).subscribe((queryParams: Params) => {
      this.dataStore.pageTitle = `${queryParams['testItemText']}评分标准 (${queryParams['ageGroupText']})`;
      this.getTestScoreCriteria(queryParams);
      this.getTestItemScoreOption(queryParams);
      this.handleTestContentDesc(queryParams);
    });
  }

  /**
   * @description 查询评分标准（教练）
   * @author ouyqx-691
   * @date 2018-4-11
   */
  getTestScoreCriteria(queryParams) {
    const params = {
      orgUserId: this.authService.getAuthOrgId(),
      itemId: queryParams['testContentId'],
      ageId: queryParams['ageGroupId']
    };
    this.teacherService.getTestScoreCriteria(params).subscribe((res) => {
      if (res.result === '0' && res.data) {
        this.gradeList = res.data.standardList;
        this.dataStore.unitName = res.data.unitName;
        this.dataStore.moduleTitle = res.data.testDesc;
      }
    });
  }

  /**
   * @description 获取测试项加减分选项
   * @author ouyqx-691
   * @date 2018-4-16
   */
  getTestItemScoreOption(queryParams) {
    const params = {
      studentId: queryParams['studentId'],
      ageDictionarieId: queryParams['ageGroupId'],
      testDateStr: '2018-04-18',
      testMethodId: queryParams['testContentId'],
      testModeId: queryParams['testItemId'],
      sportType: '3002'
    };
    this.teacherService.getTestItemScoreOption(params).subscribe((res) => {
      if (res.result === '0' && res.data) {
        if (res.data.scoureDetaiList) { // 存在加减分选项
          this.testScoreOptions = res.data.scoureDetaiList;
        }
      }
    });
  }

  handleTestContentDesc(queryParams) {
    const options = SCORE_CRITERIA_DESC_OPTIONS;

    for (let i = 0; i < options.length; i++) {
      if (queryParams['ageGroupId'] === options[i].testAgeId) {
        const testContentList = options[i].testContentList;
        for (let j = 0; j < testContentList.length; j++) {
          const testItemId = testContentList[j].testItemId;
          const testContentId = testContentList[j].testContentId
          if (queryParams['testItemId'] === testItemId && queryParams['testContentId'] === testContentId) {
            this.descInfo = testContentList[j];
          }
        }
      }
    }
  }

  ngOnInit() {
  }
}
